import { Controller } from 'egg';

class EmployeeController extends Controller {
    /**
     * 新增职员
     */
    public async createEmployee() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id: 'string',
                employee_name: 'string',
                employee_account:'string',
                employee_password:'string',
                work_unit_id: 'string',
                department_id:'string',
                position_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.employeeService.createEmployee(request.body);
        ctx.body = ctx.helper.responseSuccess('创建成功');
    }

    /**
     * 删除职员
     */
    public async deleteEmployee() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_id: 'string',
                admin_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.employeeService.deleteEmployee(request.body);
        ctx.body = ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 查找职员
     */
    public async searchEmployee() {
        const { ctx,ctx:{request} } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const page=Number(request.body.page);
        const size=Number(request.body.size);
        if(!page||page<=0){
            ctx.body = ctx.helper.responseErrorParam('页码格式不正确');
            return;
        }
        if(!size||size<=0){
            ctx.body = ctx.helper.responseErrorParam('一页显示数量不正确');
            return;
        }
        const result = await ctx.service.employeeService.searchEmployee(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 查找全部职员
     */
    public async searchAllEmployee() {
        const { ctx } = this;
        const result = await ctx.service.employeeService.searchAllEmployee();
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 根据职员id查找职员信息
     */
    public async searchEmployeeById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.employeeService.searchEmployeeById(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 修改职员
     */
    public async updateEmployee() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id:'string',
                employee_id: 'string',
                employee_name:'string',
                work_unit_id: 'string',
                department_id:'string',
                position_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.employeeService.updateEmployee(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 更新职员状态
     */
    public async updateEmployeeStatus() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id:'string',
                employee_id: 'string',
                type:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.employeeService.updateEmployeeStatus(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 注册
     */
    public async register() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_phone:'string',
                employee_password:'string',
                code:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.employeeService.register(request.body);
        ctx.body = ctx.helper.responseSuccess('注册成功',result);
    }

    /**
     * 发送短信
     */
    public async sendSMS(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                phone:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result:any=await ctx.service.employeeService.sendSMS(request.body);
        const { code } = result;
        const returnData: any = {};
        if (code == 2) {
            returnData.code = 2;
            returnData.msg = result.msg.message;
        } else {
            returnData.code = result.code;
            returnData.msg = result.msg;
        }
        ctx.body = returnData;
    }

    /**
     * 小程序登录
     */
    public async login() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_phone:'string',
                employee_password:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.employeeService.login(request.body);
        ctx.body = ctx.helper.responseSuccess('登录成功',result);
    }

    /**
     * 更新密码
     */
    public async updatePassword() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_phone:'string',
                employee_password:'string',
                code:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.employeeService.updatePassword(request.body);
        ctx.body = ctx.helper.responseSuccess('更新成功');
    }

    /**
     * 根据职员id获取职员的身份、认证状态
     */
    public async searchEmployeeStatusById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.employeeService.searchEmployeeStatusById(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功',result);
    }

    /**
     * 实名认证
     */
    public async valiateEmployee() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_id:'string',
                employee_name:'string',
                work_unit_id:'string',
                department_id:'string',
                position_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.employeeService.valiateEmployee(request.body);
        ctx.body = ctx.helper.responseSuccess('认证成功');
    }

    /**
     * 查找我的考核成绩
     */
    public async searchMyScore() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.employeeService.searchMyScore(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功',result);
    }

    /**
     * 查找单位
     */
    public async searchWorkUnit() {
        const { ctx } = this;
        const result=await ctx.service.employeeService.searchWorkUnit();
        ctx.body = ctx.helper.responseSuccess('查找成功',result);
    }

    /**
     * 根据单位id查找部门
     */
    public async searchDepartmentByWorkUnit() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                work_unit_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.employeeService.searchDepartmentByWorkUnit(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功',result);
    }

    /**
     * 根据部门id查找职位
     */
    public async searchPositionByDepartment() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                department_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.employeeService.searchPositionByDepartment(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功',result);
    }

    /**
     * 查找考核项目与规则
     */
    public async searchGradeAndRule() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.employeeService.searchGradeAndRule(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功',result);
    }

    /**
     * 检查是否有考核项目
     */
    public async checkHasProject() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result=await ctx.service.employeeService.checkHasProject(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功',result);
    }

    /**
     * 查找被考核职员列表
     */
    public async searchGradeEmployeeList() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page:'int',
                size:'int',
                employee_id:'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const page=Number(request.body.page);
        const size=Number(request.body.size);
        if(!page||page<=0){
            ctx.body = ctx.helper.responseErrorParam('页码格式不正确');
            return;
        }
        if(!size||size<=0){
            ctx.body = ctx.helper.responseErrorParam('一页显示数量格式不正确');
            return;
        }
        const result=await ctx.service.employeeService.searchGradeEmployeeList(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功',result);
    }

    /**
     * 给职员打分
     */
    public async markingEmployee(){
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                project_id:'string',
                target_employee_id:'string',
                employee_id:'string',
                grade_result_score:'string',
                position_id:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.employeeService.markingEmployee(request.body);
        ctx.body = ctx.helper.responseSuccess('打分成功');
    }
}
export default EmployeeController;